package com.gy.education.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.education.R;
import com.kankan.wheel.widget.OnWheelChangedListener;
import com.kankan.wheel.widget.WheelView;
import com.kankan.wheel.widget.adapters.ArrayWheelAdapter;

/**
 * Created by 高岳 on 2016/7/12.
 * Describe:选择地区
 */
public class AddressSelectActivity extends AddressBaseActivity implements View.OnClickListener, OnWheelChangedListener {
    private WheelView mViewProvince;
    private WheelView mViewCity;
    private WheelView mViewDistrict;
    private TextView mBtnConfirm;
    private TextView btn_quit;
    private LinearLayout ll_address_activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_gk);
        setUpViews();
        setUpListener();
        setUpData();
    }

    private void setUpViews() {
        mViewProvince = (WheelView) findViewById(R.id.id_province);
        mViewCity = (WheelView) findViewById(R.id.id_city);
        mViewDistrict = (WheelView) findViewById(R.id.id_district);
        mBtnConfirm = (TextView) findViewById(R.id.btn_confirm);
        btn_quit = (TextView) findViewById(R.id.btn_quit);
        ll_address_activity = (LinearLayout) findViewById(R.id.ll_address_activity);

        mViewProvince.setWheelBackground(R.color.white);
        mViewProvince.setWheelForeground(R.mipmap.ic_add_eat_wheel_foreground);
        mViewCity.setWheelBackground(R.color.white);
        mViewCity.setWheelForeground(R.mipmap.ic_add_eat_wheel_foreground);
        mViewDistrict.setWheelBackground(R.color.white);
        mViewDistrict.setWheelForeground(R.mipmap.ic_add_eat_wheel_foreground);
    }

    private void setUpListener() {
        // 添加change事件
        mViewProvince.addChangingListener(this);
        // 添加change事件
        mViewCity.addChangingListener(this);
        // 添加change事件
        mViewDistrict.addChangingListener(this);
        // 添加onclick事件
        mBtnConfirm.setOnClickListener(this);
        btn_quit.setOnClickListener(this);
        ll_address_activity.setOnClickListener(this);
    }

    private void setUpData() {
        initProvinceDatas();
        ArrayWheelAdapter<String> stringArrayWheelAdapter = new ArrayWheelAdapter<String>(AddressSelectActivity.this, mProvinceDatas);
        mViewProvince.setViewAdapter(stringArrayWheelAdapter);
        // 设置可见条目数量
        mViewProvince.setVisibleItems(7);
        mViewCity.setVisibleItems(7);
        mViewDistrict.setVisibleItems(7);
        updateCities();
        updateAreas();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        // TODO Auto-generated method stub
        if (wheel == mViewProvince) {
            updateCities();
        } else if (wheel == mViewCity) {
            updateAreas();
        } else if (wheel == mViewDistrict) {
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
            mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
        }
    }

    /**
     * 根据当前的市，更新区WheelView的信息
     */
    private void updateAreas() {
        int pCurrent = mViewCity.getCurrentItem();
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
        String[] areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null) {
            areas = new String[]{""};
        }
        mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
        mViewDistrict.setCurrentItem(0);
        mCurrentDistrictName = areas[0];
    }

    /**
     * 根据当前的省，更新市WheelView的信息
     */
    private void updateCities() {
        int pCurrent = mViewProvince.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[]{""};
        }
        mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
        mViewCity.setCurrentItem(0);
        updateAreas();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm:
                showSelectedResult();
                break;
            case R.id.btn_quit:
                finish();
                break;
            case R.id.ll_address_activity:
                finish();
                break;
        }
    }

    private void showSelectedResult() {
        Intent intent = new Intent();
        intent.putExtra("proviceName", mCurrentProviceName);
        intent.putExtra("cityName", mCurrentCityName);
        intent.putExtra("districtName", mCurrentDistrictName);
        intent.putExtra("zipCode", mCurrentZipCode);
        setResult(101, intent);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
        }
        return super.onKeyDown(keyCode, event);
    }
}
